package operations;

import book.Book;
import book.BookList;
import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.util.Scanner;

public class DelOperation implements IOperation {

    @Override
    public void work(BookList bookList){
        System.out.println("请输入删除的图书：");
        Scanner scan = new Scanner(System.in);
        String name = scan.nextLine();
        int index = -1;
        int currentSize = bookList.getUsedsize();
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getPos(i);
        if(name.equals(book.getName())){
                index = i;
                break;
            }
        }
        if(index == -1){
            System.out.println("没有该图书");
            return;
        }
        for (int i = index; i < currentSize-1; i++) {
            Book book = bookList.getPos(i+1);
            bookList.setBooks(book,i);
        }
        bookList.setBooks(null,currentSize-1);
        bookList.setUsedsize(currentSize-1);
        System.out.println("删除成功");
    }
}
